package com.example.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.core.Result;
import com.example.common.core.ResultGenerator;
import com.example.system.dao.AdminRoleDao;
import com.example.system.dao.RoleDao;
import com.example.system.dao.RoleResourceDao;
import com.example.system.dto.RoleParam;
import com.example.system.dto.RoleResult;
import com.example.system.model.Role;
import com.example.system.model.RoleResource;
import com.example.system.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;
    @Resource
    private RoleResourceDao roleResourceDao;
    @Resource
    private AdminRoleDao adminRoleDao;
    @Override
    public Result add(RoleParam roleParam) {
        Role role = BeanUtil.copyProperties(roleParam, Role.class);
        roleDao.insertSelective(role);
        List<Integer> resourceIds = roleParam.getResourceIds();
        if (CollUtil.isNotEmpty(resourceIds)) {
            List<RoleResource> roleResources = resourceIds.stream().map(resourceId -> new RoleResource(null, role.getId(), resourceId)).collect(Collectors.toList());
            roleResourceDao.insertList(roleResources);
        }
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result delete(Integer id) {
        return deleteBatch(Arrays.asList(id));
    }

    @Override
    public Result deleteBatch(List<Integer> ids) {
        roleDao.deleteBatchByPrimaryKeys(ids);
        roleResourceDao.deleteBatchByRoleIds(ids);
        adminRoleDao.deleteBatchByRoleIds(ids);
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result update(RoleParam roleParam) {
        Role role = BeanUtil.copyProperties(roleParam, Role.class);
        roleDao.updateByPrimaryKeySelective(role);
        List<Integer> resourceIds = roleParam.getResourceIds();
        if (CollUtil.isNotEmpty(resourceIds)) {
            roleResourceDao.deleteByRoleId(role.getId());
            List<RoleResource> roleResources = resourceIds.stream().map(resourceId -> new RoleResource(null, role.getId(), resourceId)).collect(Collectors.toList());
            roleResourceDao.insertList(roleResources);
        }
        return ResultGenerator.genSuccessResult();
    }

    @Override
    public Result select(Integer id) {
        Role role = roleDao.selectByPrimaryKey(id);
        if(ObjectUtil.isNull(role)){
            return ResultGenerator.genFailResult("角色不存在");
        }
        RoleResult roleResult = BeanUtil.copyProperties(role, RoleResult.class);
        return ResultGenerator.genSuccessResult(roleResult);
    }

    @Override
    public Result selectList(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<RoleResult> roleResults = roleDao.selectAll();
        PageInfo pageInfo = new PageInfo(roleResults);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @Override
    public Result selectRolesByAdminId(Integer id) {
        List<RoleResult> roleResults = roleDao.selectRolesByAdminId(id);
        return ResultGenerator.genSuccessResult(roleResults);
    }
}
